/*
	Stargate Vortex for GarrysMod10
	Copyright (C) 2007  aVoN

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

-- Fix, because the GmodUpdate fucked around with render.DrawSprite. The fix ix "$spriterendermode" 5
EFFECT.Material = StarGate.MaterialFromVMT(
	"StargateKawooshSprite",
	[["Sprite"
	{
		"$spriteorientation" "vp_parallel"
		"$spriteorigin" "[ 0.50 0.50 ]"
		"$basetexture" "sprites/glow1"
		"$spriterendermode" 5
	}]]
);
--################### Init @aVoN
function EFFECT:Init(data)
	local e = data:GetEntity();
	if(not (e and e:IsValid())) then return end;
	local mdl = e:GetModel();
	if(not (mdl and mdl ~= "" and mdl ~= "models/error.mdl")) then return end; -- Stops crashing ppl
	self.Entity:SetPos(e:GetPos());
	self.Entity:SetParent(e);
	self.Created = CurTime();
	self.Parent = e;
	self.Draw = true;
	-- This is the data about the Vortex. Can be altered for other gate types (aka Supergate)
	-- For more information, watch EFFECT:CalcSize()
	self.Data = {
		Time=1.3, -- Time to draw the vortex
		Base=150, -- Base or startsize
		-- Exponetial coefficients
		Exp={
			Divisor=6,
			Multiplier=3,
		},
		-- Polynomial coefficients
		Poly={
			Pow=2,
			Divisor=2,
			Multiplier=1,
		},
		Size=7.0, -- Size multiplier (liearic)
		Distance=50, -- Distance between each sprite
		Length=28, -- Length of the vortex (Amount of sprites)
		FadeStart=15, -- On what sprite shall we start fading alpha?

--		Size=6.0, -- Size multiplier (liearic)
--		Distance=50, -- Distance between each sprite
--		Length=28, -- Length of the vortex (Amount of sprites)
--		FadeStart=15, -- On what sprite shall we start fading alpha?
		
	}
	self.Draw = true;
	local offset = Vector()*512
	self.Entity:SetRenderBounds(-1*offset,offset);
end

--################# Think @aVoN
function EFFECT:Think()
	return self.Draw and self.Parent and self.Parent:IsValid();
end

--################# Draw the vortex @aVoN
function EFFECT:Render()
	if(not self.Draw) then return end; -- Stops crashing ppl
	-- Draw the vortex!
	local startsize = 0;
	if(self.Draw) then
		local time = CurTime();
		-- Kill!
		if(self.Created+self.Data.Time < time) then 
			self.Draw = nil;
			return;
		end
		local num = self.Data.Length*math.sin((CurTime()-self.Created)*math.pi/self.Data.Time);
		for i=0,num do
			local size = self:CalcSize(i,num);
			if(i == 0) then
				startsize = size/10;
			end
			local pos = self.Parent:LocalToWorld(Vector(self.Data.Distance*i+startsize,0,0));
			render.SetMaterial(self.Material);
			render.DrawSprite(pos,size,size,Color(math.random(50,150),180,math.random(150,255),255*(num+self.Data.FadeStart)/(self.Data.Length+self.Data.FadeStart)));
		end
	end
end

--################# Calculate the size depending on the datas @aVoN
function EFFECT:CalcSize(i,num)
	local size = self.Data.Size*(
		-- Base or Startsize
		self.Data.Base + 
		-- Exponetial growing
		math.exp(i/self.Data.Exp.Divisor)*self.Data.Exp.Multiplier + 
		-- Polynomial growing
		(((self.Data.Length-num)/self.Data.Poly.Divisor)^self.Data.Poly.Pow)*self.Data.Poly.Multiplier
	)
	return size;
end
